﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
namespace ChangDiTools {
    public static class GeTools {
        /// <summary>
        /// 将弧度值转换为角度值
        /// </summary>
        /// <param name="angle">弧度</param>
        /// <returns>返回角度值</returns>
        public static double RadianToDegree(this double angle)
        {
            return angle * (180.0 / Math.PI);
        }
        /// <summary>
        /// 将角度值转换为弧度值
        /// </summary>
        /// <param name="angle">角度</param>
        /// <returns>弧度</returns>
        public static double DegreeToRadian(this double angle)
        {
            return angle * Math.PI / 180.0;
        }
        /// <summary>
        /// 用极坐标方法获取点
        /// </summary>
        /// <param name="p">基点</param>
        /// <param name="dAng">角度</param>
        /// <param name="dDist">距离</param>
        /// <returns></returns>
        public static Point2d PolarPoints(Point2d p, double dAng, double dDist) {
            return new Point2d(p.X + dDist * Math.Cos(dAng)
                , p.Y + dDist * Math.Sin(dAng));
        }

        public static Point3d PolarPoints(Point3d p, double dAng, double dDis) {
            return new Point3d(p.X + dDis * Math.Cos(dAng),
                p.Y + dDis * Math.Sin(dAng),
                p.Z);
        }
        public static Point3d PolarPointsl(this Point3d p, double dAng, double dDis) {
            return new Point3d(p.X + dDis * Math.Cos(dAng),
                p.Y + dDis * Math.Sin(dAng),
                p.Z);
        }
        /// <summary>
        /// 获取直线（两点）中点
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public static Point3d MidPoints(Line line) {
            return MidPoints(line.StartPoint, line.EndPoint);
        }

        public static Point3d MidPoints(Point3d startPoint3D, Point3d endPoint3D) {
            return new Point3d((startPoint3D.X + endPoint3D.X) / 2,
                (startPoint3D.Y + endPoint3D.Y) / 2,
                (startPoint3D.Z + endPoint3D.Z) / 2);
        }
        public static Point2d MidPoints(Point2d startPoint, Point2d endPoint) {
            return new Point2d((startPoint.X + endPoint.X) / 2,
                (startPoint.Y + endPoint.Y) / 2);
        }
        /// <summary>
        /// 计算从第一点到第二点所确定的矢量与X轴正方向的夹角
        /// </summary>
        /// <param name="pt1">第一点</param>
        /// <param name="pt2">第二点</param>
        /// <returns>返回两点所确定的矢量与X轴正方向的夹角</returns>
        public static double AngleFromXAxis(this Point3d pt1, Point3d pt2)
        {
            Line line=new Line(pt1,pt2);
            return line.Angle;
        }
        /// <summary>
        /// 根据外接矩形创建椭圆
        /// </summary>
        /// <param name="ellipse">椭圆对象</param>
        /// <param name="pt1">外接矩形的角点</param>
        /// <param name="pt2">外接矩形的角点</param>
        public static void CreateEllipse(this Ellipse ellipse, Point3d pt1, Point3d pt2)
        {
            Point3d center = MidPoints(pt1, pt2);
            Vector3d normal=Vector3d.ZAxis;
            Vector3d majorAxis=new Vector3d(Math.Abs(pt1.X-pt2.X)/2,0,0);
            double ratio = Math.Abs((pt1.Y - pt2.Y) / (pt1.X - pt2.X));
            ellipse.Set(center,normal,majorAxis,ratio,0,2*Math.PI);
        }
    }
}
